/*
 * tmf8801_4.h:
 * Copyright (c) 2014-2021 Rtrobot. <admin@rtrobot.org>
 *  <http://rtrobot.org>
 ***********************************************************************
 * use keil5 for arm.
 tmf8801_4 ---->  tmf8801_4
 
 
 */
#ifndef __TMF8001_4_H_
#define __TMF8001_4_H_

#include "main.h"


#define false 0
#define true  1

#define   TMF8x01_OK   1
#define   TMF8x01_NK   0

#define tmf8801_4_I2CADDR      0x41




//Calibration and Algorithm State Data Exchange
#define tmf8801_4_CALIB_ADDRESS 0x20     
#define tmf8801_4_STATE_DATA_WR 0x2E

//tmf8801_4 Register Description
#define tmf8801_4_APPID_REG 0x00            

#define tmf8801_4_APPREV_MAJOR_REG 0x01     
#define tmf8801_4_APPREQID_REG 0x02     

#define tmf8801_4_CMD_DATA9_REG 0x06
#define tmf8801_4_CMD_DATA8_REG 0x07

#define tmf8801_4_CMD_DATA7_REG 0x08
#define tmf8801_4_CMD_DATA6_REG 0x09
#define tmf8801_4_CMD_DATA5_REG 0x0A
#define tmf8801_4_CMD_DATA4_REG 0x0B
#define tmf8801_4_CMD_DATA3_REG 0x0C
#define tmf8801_4_CMD_DATA2_REG 0x0D
#define tmf8801_4_CMD_DATA1_REG 0x0E

#define tmf8801_4_CMD_DATA0_REG 0x0F

#define tmf8801_4_COMMAND_REG 0x10        			//
//tmf8801_4_APPID_REG ==0XC0  ʱ���ſ��Լ���
//02 ==





#define tmf8801_4_PREVIOUS_REG 0x11
#define tmf8801_4_APPREV_MINOR_REG 0x12
#define tmf8801_4_APPREV_PATCH_REG 0x13
#define tmf8801_4_STATUS_STATUS_REG 0x1D
#define tmf8801_4_REGISTER_CONTENTS_REG 0x1E
#define tmf8801_4_TID_REG 0x1F
#define tmf8801_4_RESULT_NUMBER_REG 0x20          //
#define tmf8801_4_RESULT_INFO_REG 0x21            //

#define tmf8801_4_DISTANCE_PEAK_0_REG 0x22
#define tmf8801_4_DISTANCE_PEAK_1_REG 0x23

#define tmf8801_4_SYS_CLOCK_0_REG 0x24
#define tmf8801_4_SYS_CLOCK_1_REG 0x25

#define tmf8801_4_SYS_CLOCK_2_REG 0x26
#define tmf8801_4_SYS_CLOCK_3_REG 0x27


#define tmf8801_4_STATE_DATA_0_REG 0x28
#define tmf8801_4_STATE_DATA_1_REG 0x29
#define tmf8801_4_STATE_DATA_2_REG 0x2A
#define tmf8801_4_STATE_DATA_3_REG 0x2B
#define tmf8801_4_STATE_DATA_4_REG 0x2C
#define tmf8801_4_STATE_DATA_5_REG 0x2D
#define tmf8801_4_STATE_DATA_6_REG 0x2E
#define tmf8801_4_STATE_DATA_7_REG 0x2F
#define tmf8801_4_STATE_DATA_8_XTALK_MSB_REG 0x30
#define tmf8801_4_STATE_DATA_9_XTALK_LSB_REG 0x31
#define tmf8801_4_STATE_DATA_10_TJ_REG 0x32
#define tmf8801_4_REFERENCE_HITS_0_REG 0x33
#define tmf8801_4_REFERENCE_HITS_1_REG 0x34
#define tmf8801_4_REFERENCE_HITS_2_REG 0x35
#define tmf8801_4_REFERENCE_HITS_3_REG 0x36
#define tmf8801_4_OBJECT_HITS_0_REG 0x37
#define tmf8801_4_OBJECT_HITS_1_REG 0x38
#define tmf8801_4_OBJECT_HITS_2_REG 0x39
#define tmf8801_4_OBJECT_HITS_3_REG 0x3A


#define tmf8801_4_ENABLE_REG 						0xE0   //ʹ�ܼĴ���   ENABLE Register (Address 0xE0)
//bit 7  cpu_reset      д1��λоƬ
//bit 6  cpu_ready      CPU �Ƿ�׼����λ�� ��λ����Ϊ1�� �̼����ܹ���
//bit 0  pon            1=��������0=Ҫ��cpu�������״̬  

#define tmf8801_4_INT_STATUS_REG 				0xE1
#define tmf8801_4_INT_ENAB_REG 				0xE2				     //�ж�ʹ�ܼĴ���
#define tmf8801_4_ID_REG                			0xE3                      //оƬID����ȡ07h-
#define tmf8801_4_REVID_REG 0xE4


//*******************************Bootloader Registers***************
//Following registers are only available if appid=0x80 (Bootloader): 
//���µļĴ��� ֻ����appid=0x80  ʱ �ſ����á�   Ҳ����Bootloader ��״̬�²�����

#define  BL_CMD_STAT    0x08    //���д�� ����Bootloader Command  �� ������� == Bootloader Status

//Bootloader Command
	#define RAMREMAP_RESET   0x11    		//Remap RAM to Address 0 and Reset    ��λ
	#define DOWNLOAD_INIT  	 0x14  			//Initialize for RAM download from host to tmf8801_4   //��ʼ��RAM ,������TO tmf8801_4
    #define W_RAM  			  0x41             //Write RAM Region (Plain = not encoded into e.g. Intel Hex
    #define ADDR_RAM  			0x43     	//Set the read/write RAM pointer to a given address   ��дRMA���׵�ַ
	

#define  BL_SIZE  		0x09     //����
//#define  BL_DATA          (Address 0x0A-0x8A)  Up to 128 data bytes for bootloader commands
#define  BL_CSUM 		0x8B    //У����




void TMF8801_4_GPIO_0 (unsigned char i);


int8_t tmf8801_4_init (void);
 int8_t __tmf8801_4_rst (void); 
 int8_t __tmf8801_4_status_read (void);
  int8_t __tmf8801_4_download_init (void);
   int8_t __tmf8801_4_set_ram_addr (uint16_t addr);
    int8_t __tmf8801_4_write_ram (void);
	int8_t __tmf8801_4_ram_remap_rst (void);
	// int8_t __tmf8801_4_info_read (void);
	  int8_t __tmf8801_4_info_read (void);
	 
int8_t tmf8801_4_app0_start (void);	 
	

unsigned char  tmf8801_4_APP_Init(void);

uint32_t tmf8801_4_Get_Distance(void);

void tmf8801_4_Clear_Interrupt(void);
void tmf8801_4_Set_Interrupt(unsigned char  flag);
unsigned char  tmf8801_4_ResetDevice(void);
unsigned char  tmf8801_4_Run_App(void);

unsigned char  tmf8801_4_Get_Factory_calibration(void);

void tmf8801_4_Set_Factory_calibration(void);

unsigned char  tmf8801_4_Wait_CpuReady(void);
unsigned char  I2C_4_TMF8001_ReadByte(uint8_t reg_addr, uint8_t *pdata, uint8_t length);
unsigned char  tmf8801_4_ReadBit(uint8_t reg, uint8_t bit);
unsigned char  I2C_4_TMF8001_WriteByte(uint8_t reg_addr, uint8_t *reg_data, uint16_t length);
void tmf8801_4_ChangeI2cAddr(unsigned char addr);



#endif /* __TMF8001_H_ */


